package test.client;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Date;
import java.util.List;

import javax.swing.JTextArea;

import org.je.util.DateUtil;
import org.je.util.StringUtil;

public class ChartText extends JTextArea implements KeyListener
{
	private static final long	serialVersionUID	= 1L;
	private boolean				isCtrl				= false;

	public ChartText() {
		this.addKeyListener(this);
		this.setForeground(new Color(0, 150, 0));
		this.setFont(new Font("微软雅黑", Font.BOLD, 12));
		this.setText("@host@127.0.0.1@9999");
		this.setLineWrap(true);
	}

	private void dealMsg(String msg) {
		if (msg.startsWith("@host")) {
			List<String> list = StringUtil.split(msg, "@");
			UIShared.chartUI.ip = list.get(1);
			UIShared.chartUI.port = Integer.parseInt(list.get(2));
			UIShared.chartUI.setTitle("聊天界面：[服务器地址 > " + UIShared.chartUI.ip + ":" + UIShared.chartUI.port + "]");
		} else if (msg.startsWith("@login")) {
			if (UIShared.chartUI.isConnected() == false)
				UIShared.chartUI.connect();
			List<String> list = StringUtil.split(msg, "@");
			UIShared.chartUI.login(list.get(1), list.get(2), list.get(3));
		} else if (msg.startsWith("@logout")) {
			UIShared.chartUI.logout();
			UIShared.chartUI.disconnect();
		} else {
			UIShared.chartUI.chart(DateUtil.fromat(new Date()) + "#" + msg);
		}
	}

	public void keyReleased(KeyEvent e) {
		if (e.getKeyCode() == 17) {
			isCtrl = false;
		}
	}

	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == 17) {
			isCtrl = true;
		} else if (isCtrl && e.getKeyCode() == 10) {
			String msg = UIShared.text.getText().trim();
			UIShared.text.setText("");
			dealMsg(msg);
		}
	}

	public void keyTyped(KeyEvent e) {}

}
